Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add csv export for assets #1392

Merged
merged 4 commits into from
Jan 22, 2025
Merged

feat: add csv export for assets #1392

merged 4 commits into from
Jan 22, 2025

Conversation

ab-smith
Copy link
Contributor

@ab-smith ab-smith commented Jan 21, 2025

  • backend ready
  • expose the export button

Summary by CodeRabbit

Release Notes

  • New Features

    • Added CSV export functionality for assets.
    • Expanded export capabilities across different models.
  • UI Improvements

    • Introduced new button style .btn-mini-tertiary.
    • Updated export button visibility for multiple models.
  • Backend Enhancements

    • Implemented dynamic export endpoint supporting different model types.

The release introduces more flexible data export options and refined UI styling, improving user interaction and data accessibility.

Copy link

coderabbitai bot commented Jan 21, 2025

Walkthrough

This pull request introduces a new CSV export functionality for assets in the backend and frontend components. A new method, export_csv, has been added to the AssetViewSet class to generate CSV files containing asset data. The frontend has been updated to accommodate dynamic model-based exports, enhancing the visibility of export options and updating button styles accordingly. These changes collectively improve the user experience by providing a flexible and consistent interface for exporting asset information.

Changes

File Change Summary
backend/core/views.py Added export_csv method to AssetViewSet for generating CSV exports of asset data.
frontend/src/app.postcss Introduced new .btn-mini-tertiary CSS class for button styling.
frontend/src/routes/(app)/(internal)/[model=urlmodel]/+page.svelte Updated conditional rendering and button styling for broader export functionality.
frontend/src/routes/(app)/(internal)/[model=urlmodel]/export/+server.ts Modified export endpoint to support dynamic model-based CSV exports.
frontend/src/routes/(app)/(third-party)/[model=thirdparty_urlmodels]/+page.svelte Expanded export button visibility conditions to include additional models.

Sequence Diagram

sequenceDiagram
    participant User
    participant Frontend
    participant Backend
    participant Database
    
    User->>Frontend: Request CSV Export
    Frontend->>Backend: GET /export with model
    Backend->>Database: Fetch Accessible Assets
    Database-->>Backend: Return Asset Data
    Backend->>Backend: Generate CSV
    Backend-->>Frontend: CSV Response
    Frontend-->>User: Download CSV File
Loading

Poem

🐰 Hop, hop, export we go!
CSV magic starts to flow
Assets dancing, data bright
From backend to frontend's delight
A rabbit's code, precise and neat 🌟

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (4)
frontend/src/routes/(app)/(internal)/[model=urlmodel]/export/+server.ts (1)

Line range hint 14-21: Sanitize the filename to ensure compatibility.

The current filename includes special characters from toISOString() which might cause issues. Consider sanitizing it.

-	const fileName = `${URLModel}-${new Date().toISOString()}.csv`;
+	const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
+	const fileName = `${URLModel}-${timestamp}.csv`;
 
 	return new Response(await res.blob(), {
 		headers: {
 			'Content-Type': 'text/csv',
 			'Content-Disposition': `attachment; filename="${fileName}"`
 		}
 	});
frontend/src/routes/(app)/(third-party)/[model=thirdparty_urlmodels]/+page.svelte (1)

Line range hint 69-75: Consider extracting shared export button logic.

The export button condition ['applied-controls', 'assets'].includes(URLModel) is duplicated across components. Consider extracting this into a shared constant or utility function.

Also, enhance accessibility by adding an aria-label to the export button:

 <a
   href="{URLModel}/export/"
   class="inline-block p-3 text-gray-50 bg-pink-500 hover:bg-pink-400 w-12 focus:relative"
   title={m.exportButton()}
+  aria-label={m.exportButton()}
   data-testid="export-button"><i class="fa-solid fa-download mr-2" /></a
 >
frontend/src/routes/(app)/(internal)/[model=urlmodel]/+page.svelte (1)

Line range hint 150-157: Align button styling with third-party component.

The export button uses different styling classes between internal and third-party components. Consider using consistent styling:

  • Internal: btn-mini-tertiary
  • Third-party: text-gray-50 bg-pink-500 hover:bg-pink-400

Also, add aria-label for accessibility:

 <a
   href="{URLModel}/export/"
   class="inline-block p-3 btn-mini-tertiary w-12 focus:relative"
   title={m.exportButton()}
+  aria-label={m.exportButton()}
   data-testid="export-button"><i class="fa-solid fa-download mr-2" /></a
 >
backend/core/views.py (1)

511-550: Implementation looks good with some suggestions for improvement.

The CSV export functionality is well-implemented with proper permission checks and data handling. Consider these improvements:

  1. For large datasets, consider implementing pagination or streaming to reduce memory usage.
  2. Add error handling for potential exceptions during CSV generation.

Example implementation with streaming and error handling:

@action(detail=False, name="Export assets as CSV")
def export_csv(self, request):
    try:
        (viewable_assets_ids, _, _) = RoleAssignment.get_accessible_object_ids(
            Folder.get_root_folder(), request.user, Asset
        )
        response = HttpResponse(content_type="text/csv")
        response["Content-Disposition"] = 'attachment; filename="assets_export.csv"'

        writer = csv.writer(response, delimiter=";")
        columns = [
            "internal_id",
            "name",
            "description",
            "type",
            "security_objectives",
            "disaster_recovery_objectives",
            "link",
            "owners",
            "parent_assets",
            "labels",
        ]
        writer.writerow(columns)

+       # Use iterator() to reduce memory usage
+       for asset in Asset.objects.filter(id__in=viewable_assets_ids).iterator():
            row = [
                asset.id,
                asset.name,
                asset.description,
                asset.type,
                asset.get_security_objectives_display(),
                asset.get_disaster_recovery_objectives_display(),
                asset.reference_link,
                ",".join([o.email for o in asset.owner.all()]),
                ",".join([o.name for o in asset.parent_assets.all()]),
                ",".join([o.label for o in asset.filtering_labels.all()]),
            ]
            writer.writerow(row)

        return response
+   except Exception as e:
+       logger.error(f"Error exporting assets to CSV: {str(e)}")
+       return HttpResponse(
+           status=500,
+           content="An error occurred while generating the CSV export."
+       )
🧰 Tools
🪛 Ruff (0.8.2)

514-514: Asset may be undefined, or defined from star imports

(F405)


534-534: Asset may be undefined, or defined from star imports

(F405)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 25269f7 and 1924b1f.

📒 Files selected for processing (5)
  • backend/core/views.py (1 hunks)
  • frontend/src/app.postcss (1 hunks)
  • frontend/src/routes/(app)/(internal)/[model=urlmodel]/+page.svelte (1 hunks)
  • frontend/src/routes/(app)/(internal)/[model=urlmodel]/export/+server.ts (1 hunks)
  • frontend/src/routes/(app)/(third-party)/[model=thirdparty_urlmodels]/+page.svelte (1 hunks)
🧰 Additional context used
🪛 Ruff (0.8.2)
backend/core/views.py

514-514: Asset may be undefined, or defined from star imports

(F405)


534-534: Asset may be undefined, or defined from star imports

(F405)

⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: startup-docker-compose-test
  • GitHub Check: enterprise-functional-tests (3.12, chromium)
  • GitHub Check: test (3.12)
  • GitHub Check: functional-tests (3.12, chromium)
🔇 Additional comments (2)
frontend/src/app.postcss (1)

27-29: LGTM! The new button style follows existing patterns.

The .btn-mini-tertiary class maintains consistency with existing button styles while providing good contrast and hover states.

frontend/src/routes/(app)/(internal)/[model=urlmodel]/+page.svelte (1)

Line range hint 158-164: LGTM! Graph visualization button is well-implemented.

The graph visualization button for assets is properly implemented with consistent styling and clear purpose.

Comment on lines +5 to 13
export const GET: RequestHandler = async ({ params, fetch }) => {
const URLModel = params.model;
const endpoint = `${BASE_API_URL}/${URLModel}/export_csv/`;

const res = await fetch(endpoint);
if (!res.ok) {
error(400, 'Error fetching the CSV file');
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance error handling and add model validation.

The error handling could be more specific to help debug issues. Also, consider validating the model parameter.

 export const GET: RequestHandler = async ({ params, fetch }) => {
 	const URLModel = params.model;
+	const validModels = ['applied-controls', 'assets'];
+	if (!validModels.includes(URLModel)) {
+		error(400, `Invalid model: ${URLModel}`);
+	}
+
 	const endpoint = `${BASE_API_URL}/${URLModel}/export_csv/`;
 
 	const res = await fetch(endpoint);
 	if (!res.ok) {
-		error(400, 'Error fetching the CSV file');
+		error(res.status, `Failed to fetch CSV: ${await res.text()}`);
 	}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export const GET: RequestHandler = async ({ params, fetch }) => {
const URLModel = params.model;
const endpoint = `${BASE_API_URL}/${URLModel}/export_csv/`;
const res = await fetch(endpoint);
if (!res.ok) {
error(400, 'Error fetching the CSV file');
}
export const GET: RequestHandler = async ({ params, fetch }) => {
const URLModel = params.model;
const validModels = ['applied-controls', 'assets'];
if (!validModels.includes(URLModel)) {
error(400, `Invalid model: ${URLModel}`);
}
const endpoint = `${BASE_API_URL}/${URLModel}/export_csv/`;
const res = await fetch(endpoint);
if (!res.ok) {
error(res.status, `Failed to fetch CSV: ${await res.text()}`);
}

@ab-smith ab-smith changed the title export assets csv feat: add csv export on assets view Jan 21, 2025
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
backend/core/views.py (2)

511-564: Good implementation with room for improvement.

The CSV export implementation is solid with proper permission handling, error handling, and memory-efficient iteration. Consider these enhancements:

  1. Make the filename more descriptive by including a timestamp and domain/folder name.
  2. Allow configurable CSV delimiter through settings or request parameters.
  3. Add content encoding specification to handle special characters correctly.
  4. Consider implementing progress tracking for large datasets.

Here's a suggested improvement:

-            response["Content-Disposition"] = 'attachment; filename="assets_export.csv"'
+            timestamp = timezone.now().strftime("%Y%m%d_%H%M%S")
+            filename = f"assets_export_{timestamp}.csv"
+            response["Content-Disposition"] = f'attachment; filename="{filename}"'
+            response["Content-Type"] = "text/csv; charset=utf-8"
🧰 Tools
🪛 Ruff (0.8.2)

515-515: Asset may be undefined, or defined from star imports

(F405)


535-535: Asset may be undefined, or defined from star imports

(F405)


Line range hint 1-1: Replace star imports with explicit imports for better maintainability.

The static analysis tool indicates that Asset may be undefined. While it's imported through .models import *, explicit imports make dependencies clearer and improve code maintainability.

Replace the star import with explicit imports:

-from .models import *
+from .models import (
+    Asset,
+    Evidence,
+    FilteringLabel,
+    # Add other required models...
+)
🧰 Tools
🪛 Ruff (0.8.2)

509-509: Asset may be undefined, or defined from star imports

(F405)


515-515: Asset may be undefined, or defined from star imports

(F405)


535-535: Asset may be undefined, or defined from star imports

(F405)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1924b1f and 92bfd22.

📒 Files selected for processing (1)
  • backend/core/views.py (1 hunks)
🧰 Additional context used
🪛 Ruff (0.8.2)
backend/core/views.py

515-515: Asset may be undefined, or defined from star imports

(F405)


535-535: Asset may be undefined, or defined from star imports

(F405)

⏰ Context from checks skipped due to timeout of 90000ms (10)
  • GitHub Check: startup-functional-test (3.12)
  • GitHub Check: startup-docker-compose-test
  • GitHub Check: functional-tests (3.12, chromium)
  • GitHub Check: enterprise-startup-functional-test (3.12)
  • GitHub Check: test (3.12)
  • GitHub Check: enterprise-startup-docker-compose-test
  • GitHub Check: Analyze (python)
  • GitHub Check: enterprise-functional-tests (3.12, chromium)
  • GitHub Check: build (3.12)
  • GitHub Check: Analyze (javascript-typescript)

@ab-smith ab-smith changed the title feat: add csv export on assets view feat: add csv export for assets Jan 22, 2025
Copy link
Collaborator

@eric-intuitem eric-intuitem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ab-smith ab-smith merged commit 2f1badb into main Jan 22, 2025
20 checks passed
@ab-smith ab-smith deleted the export_assets_csv branch January 22, 2025 17:34
@github-actions github-actions bot locked and limited conversation to collaborators Jan 22, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants